<?php
namespace Modules\User\Controllers;

use Core\Mvc\Controller;
use Modules\User\Entity\User as UserModel;
use MongoDB\BSON\ObjectID;

class IndexController extends Controller
{
    public function indexAction($id)
    {
        $user = $this->entityManager->get('user', [
            'conditions' => [
                '_id' => new ObjectID($id)
            ]
        ]);
        if (!$user) {
            return $this->notFount();
        }
        self::$variables['title'] = $user->name . ' 的主页';
        self::$variables['description'] = $user->name . ' 的主页';
        self::$variables['keywords'] = $user->name;
        self::$variables += [
            '#templates' => 'pageUser',
            'data' => $user,
        ];
    }

    public function remoteAction()
    {
        extract(self::$variables['router_params']);
        self::$variables['page'] = [
            '#templates' => 'remote',
        ];
        $this->response->setContentType('application/json', 'UTF-8');
        $username = $this->request->getPost('username');
        $user = UserModel::findFirstByName($username);
        if ($user) {
            $valid = false;
        } else {
            $valid = true;
        }
        echo json_encode(['valid' => $valid]);
    }

    public function loginAction()
    {
        self::$variables['keywords'] = self::$variables['description'] = self::$variables['title'] = '用户登陆';
        $loginForm = $this->form->create('user.loginForm');
        if ($loginForm->isValid()) {
            $state = $loginForm->save();
            if ($state !== false) {
                return $this->temMoved();
            }
        }
        if ($this->user->isLogin()) {
            return $this->temMoved(['for' => 'index']);
        }
        self::$variables += [
            '#templates' => 'login',
            'data' => $loginForm->renderForm(),
        ];
    }

    public function checkEmailAction()
    {
        self::$variables['keywords'] = self::$variables['description'] = self::$variables['title'] = '确认邮箱';
        $passwordForm = $this->config->get('user.checkEmail');
        $passwordForm = $this->form->create($passwordForm);
        self::$variables['page'] = [
            '#templates' => 'page',
        ];
        self::$variables['page']['loginForm'] = $passwordForm;
    }

    public function passwordAction()
    {
        self::$variables['keywords'] = self::$variables['description'] = self::$variables['title'] = '找回密码';
        $passwordForm = $this->config->get('user.passwordForm');
        $passwordForm = $this->form->create($passwordForm);
        if ($passwordForm->isValid()) {
            //发送邮件
            $this->temMoved(['for' => 'resetPassword']);
        }
        self::$variables += [
            '#templates' => 'password',
        ];
        self::$variables['data'] = $passwordForm->renderForm();
    }

    public function resetPasswordAction()
    {
        self::$variables['keywords'] = self::$variables['description'] = self::$variables['title'] = '找回密码';
        $passwordForm = $this->config->get('user.resetPasswordForm');
        $passwordForm = $this->form->create($passwordForm);
        if ($passwordForm->isValid()) {
            //发送邮件
            $passwordForm->save();
        }
        self::$variables += [
            '#templates' => 'resetPassword',
        ];
        self::$variables['data'] = $passwordForm->renderForm();
    }

    public function registerAction($type)
    {
        if ($this->user->isLogin()) {
            return $this->temMoved();
        }

        $registerType = $this->config->cache('registerType');
        if (!isset($registerType[$type])) {
            return $this->notFount();
        }

        $registerInfo = $registerType[$type];
        $registerForm = $this->form->create($registerInfo['field']);
        if ($registerForm->isValid()) {
            $state = $registerForm->save();
            if ($state) {
                $this->user->login($state);
                $this->flashSession->success('注册成功，并已为您自动登录');
                return $this->temMoved(['for' => 'index']);
            } else {
                $this->flash->error('注册失败');
            }
        }

        self::$variables['#templates'] = 'register';

        self::$variables['keywords'] = self::$variables['title'] = $registerInfo['name'];
        self::$variables['description'] = $registerInfo['description'];
        self::$variables['data'] = $registerForm->renderForm();
    }
}
